//
//  BMWShareManager-Extension.swift
//  BMWShareSDK
//
//  Created by 李胜兵 on 2019/6/15.
//  Copyright © 2019年 Li Shengbing, (Shengbing.Li@partner.bmw.com). All rights reserved.
//

import UIKit

extension BMWShareManager {
    
    
    /// goto weiixn
    ///
    /// - Parameters:
    ///   - contents: [BMWShareManager.ContentKey: Any]
    ///   - scene: WXScene
    
    static func gotoWeixin(_ contents: [BMWShareManager.ContentKey: Any], _ scene: WXScene) {
        if !self.prepareShareAlert(platformType: .weixin) {
            return
        }
        var type: ShareWXType = ShareWXType.textOnly(text: "")
        self.regularExpressionTypeFromContents(
            contents: contents,
            fileBlock: { (text, des, image, data) in
                type = ShareWXType.file(text: text, description: des, image: image, fileData: data)
        },
            vedioBlock: { (text, des, image, previewImageUrl, vedeoUrl, vedioLowBandUrl) in
                type = ShareWXType.vedio(text: text, description: des, image: image, vedioUrl: vedeoUrl, vedioLowBandUrl: vedioLowBandUrl)
        },
            audioBlock: { (text, des, image, previewImageUrl, audioUrl, audioDataUrl) in
                type = ShareWXType.audio(text: text, description: des, audioUrl: audioUrl, audioDataUrl: audioDataUrl)
        },
            webBlock: { (text, des, previewImageUrl, image, url) in
                type = ShareWXType.urlLink(text: text, description: des, image: image, url: url)
        },
            imageBlock: { (text, des, image, previewImage) in
                type = ShareWXType.imageOnly(image: image)
        }) { (text) in
            type = ShareWXType.textOnly(text: text)
        }
        ShareWX.share(type: type, platform: scene)
    }
    
    
    
    /// goto QQ
    ///
    /// - Parameters:
    ///   - contents: [BMWShareManager.ContentKey: Any]
    ///   - scene: BMWShareQQToWhere
    ///   - platform: ShareDestType
    
    static func gotoQQ(_ contents: [BMWShareManager.ContentKey: Any], _ scene: ShareQQPlatform, _ platform: ShareDestType) {
        if !self.prepareShareAlert(platformType: .qq) {
            return
        }
        var type: ShareQQType = ShareQQType.textOnly(text: "")
        self.regularExpressionTypeFromContents(
            contents: contents,
            fileBlock: { (text, des, image, data) in
                
        },
            vedioBlock: { (text, des, image, previewImageUrl, videoUrl, vedioLowBandUrl) in
                type = ShareQQType.video(videoUrl: videoUrl, videoPreviewImageUrl: previewImageUrl, videoPreviewImage: image, text: text, description: des)
        },
            audioBlock: { (text, des, image, previewImageUrl, audioUrl, audioDataUrl) in
                type = ShareQQType.audio(audioUrl: audioUrl, audioPreviewImageUrl: previewImageUrl, audioPreviewImage: image, text: text, description: des)
        },
            webBlock: { (text, des, previewImageUrl, image, url) in
                type = ShareQQType.urlNews(url: url, previewImageUrl: previewImageUrl, text: text, description: des)
        },
            imageBlock: { (text, des, image, previewImage) in
                type = ShareQQType.imageOnly(originImage: image, previewImage: previewImage, text: text, description: des)
        }) { (text) in
            type = ShareQQType.textOnly(text: text)
        }
        ShareQQ.share(type: type, platform: scene, shareDestType: platform)
    }
    
    
    
    /// goto weibo
    ///
    /// - Parameter contents: [BMWShareManager.ContentKey: Any]
    
    static func gotoWeibo(_ contents: [BMWShareManager.ContentKey: Any]) {
        if !self.prepareShareAlert(platformType: .weibo) {
           return
        }
        var type: ShareWeiboType = ShareWeiboType.text(text: "")
        self.regularExpressionTypeFromContents(
            contents: contents,
            fileBlock: { (text, des, image, data) in
                
        },
            vedioBlock: { (text, des, image, previewImageUrl, vedeoUrl, vedioLowBandUrl) in
                
        },
            audioBlock: { (text, des, image, previewImageUrl, audioUrl, audioDataUrl) in
                
        },
            webBlock: { (text, des, previewImageUrl, image, url) in
                type = ShareWeiboType.url(url: url!, text: text, description: des, thumbImage: image)
        },
            imageBlock: { (text, des, image, previewImage) in
                type = ShareWeiboType.image(image: image!, text: text)
        }) { (text) in
            type = ShareWeiboType.text(text: text)
        }
        ShareWeibo.shareInstance.share(type: type)
    }
    
    
    /// RegularExpression for find share way.
    ///
    /// - Parameters:
    ///   - contents: [BMWShareManager.ContentKey: Any]
    ///   - fileBlock:  (text, description, image, fileData)
    ///   - vedioBlock: (text, description, image, vedioPreviewImageUrl, vedioUrl, vedioLowBandUrl)
    ///   - audioBlock: (text, description, image, audioPreviewImageUrl, audioUrl, audioDataUrl)
    ///   - webBlock:   (text, description, previewImageUrl, image, url)
    ///   - imageBlock: (text, description, image, previewImage)
    ///   - textBlock:  (text)
    
    static func regularExpressionTypeFromContents(
        contents: [BMWShareManager.ContentKey: Any],
        fileBlock: ((_ text: String?, _ description: String?, _ image: UIImage?, _ fileData: Data?) -> Void)? = nil,
        vedioBlock: ((_ text: String?, _ description: String?, _ image: UIImage?, _ vedioPreviewImageUrl: String?,  _ vedioUrl: String, _ vedioLowBandUrl: String?) -> Void)? = nil,
        audioBlock: ((_ text: String?, _ description: String?, _ image: UIImage?, _ audioPreviewImageUrl: String?, _ audioUrl: String, _ audioDataUrl: String?) -> Void)? = nil,
        webBlock: (_ text: String?, _ description: String?, _ previewImageUrl: String?, _ image: UIImage, _ url: String?) -> Void,
        imageBlock: (_ text: String?, _ description: String?, _ image: UIImage?, _ previewImage: UIImage?) -> Void,
        textBlock: (_ text: String) -> Void
        ){
        
        let text = contents[BMWShareManager.ContentKey.text] as? String ?? ""
        let description = contents[BMWShareManager.ContentKey.description] as? String ?? ""
        let image = contents[BMWShareManager.ContentKey.image] as? UIImage ?? UIImage()
        let previewImage = contents[BMWShareManager.ContentKey.previewImage] as? UIImage ?? UIImage()
        let previewImageUrl = contents[BMWShareManager.ContentKey.previewImageUrl] as? String ?? ""
        let webUrl = contents[BMWShareManager.ContentKey.url] as? String ?? ""
        let audioUrl = contents[BMWShareManager.ContentKey.audioUrl] as? String ?? ""
        let audioDataUrl = contents[BMWShareManager.ContentKey.audioDataUrl] as? String ?? ""
        let videoUrl = contents[BMWShareManager.ContentKey.videoUrl] as? String ?? ""
        let videoLowBandUrl = contents[BMWShareManager.ContentKey.videoLowBandUrl] as? String ?? ""
        let fileData = contents[BMWShareManager.ContentKey.fileData] as? Data ?? Data()
        
        let ContentKeys = contents.keys
        
        // 文件: _ fileData: Data?, _ text: String?, _ image: UIImage?, _ description: String?
        if ContentKeys.contains(BMWShareManager.ContentKey.fileData) && fileData != Data() {
            fileBlock!(text, description, image, fileData)
            return
        }
        
        // 视频: _ text: String?, _ description: String?, _ image: UIImage?, _ vedioPreviewImageUrl: String?,  _ vedioUrl: String, _ vedioLowBandUrl: String?
        if ContentKeys.contains(BMWShareManager.ContentKey.videoUrl) && videoUrl.count != 0 {
            vedioBlock!(text, description, image, previewImageUrl, videoUrl, videoLowBandUrl)
            return
        }
        
        // 音频: _ text: String?, _ description: String?, _ image: UIImage?, _ audioPreviewImageUrl: String?, _ audioUrl: String, _ audioDataUrl: String?
        if ContentKeys.contains(BMWShareManager.ContentKey.audioUrl) && audioUrl.count != 0 {
            audioBlock!(text, description, image, previewImageUrl, audioUrl, audioDataUrl)
            return
        }
        
        // 网页: _ text: String?, _ description: String?, _ previewImageUrl: String?, _ image: UIImage, _ url: String?
        if ContentKeys.contains(BMWShareManager.ContentKey.url) && webUrl.count != 0 {
            webBlock(text, description, previewImageUrl, image, webUrl)
            return
        }
        
        // 图片: _ image: UIImage?, _ previewImage: UIImage?, _ text: String?, _ description: String?
        if ContentKeys.contains(BMWShareManager.ContentKey.image) && image != UIImage() {
            imageBlock(text, description, image, previewImage)
            return
        }
        
        // 文字: text
        if ContentKeys.contains(BMWShareManager.ContentKey.text) && text.count != 0 {
            textBlock(text)
            return
        }
    }
    
    
    static func prepareShareAlert(platformType: BMWPlatform) -> Bool {
        //guard let windowRootViewController = UIApplication.shared.keyWindow?.rootViewController else { return true }
        guard let windowRootViewController = self.topViewController() else {
            return true
        }
        
        var mainTitle = ""
        let leftTitle = BMWNSLocalizedString.localizedString("BMWSHARESDK_PLATFORM_URLSCHEME_BUTTON_TITLE", comment: "OK")
        var isExecute: Bool = false
        switch platformType {
        case .weixin:
            mainTitle = BMWNSLocalizedString.localizedString("BMWSHARESDK_WX_URLSCHEME_TITLE", comment: "Please install WeChat first.")
            if !UIApplication.shared.canOpenURL(URL(string: BMWPlatform.weixin.urlScheme)!) && !UIApplication.shared.canOpenURL(URL(string: BMWPlatform.pengyouquan.urlScheme)!) {
               isExecute = true
            }
            
        case .qq:
            mainTitle = BMWNSLocalizedString.localizedString("BMWSHARESDK_QQ_URLSCHEME_TITLE", comment: "Please install QQ first.")
            if !UIApplication.shared.canOpenURL(URL(string: BMWPlatform.qq.urlScheme)!) {
                isExecute = true
            }
            
        case .weibo:
            mainTitle = BMWNSLocalizedString.localizedString("BMWSHARESDK_WB_URLSCHEME_TITLE", comment: "Please install Weibo first.")
            if !UIApplication.shared.canOpenURL(URL(string: BMWPlatform.weibo.urlScheme)!) {
                isExecute = true
            }
            
        default:
            break
        }
        
        if isExecute {
            windowRootViewController.showAlertViewController(leftHandler: nil, rightHandler: nil, mainTitle: mainTitle, message: nil, leftTitle: leftTitle, rightTitle: nil)
            return false
        }
        return true
    }
    
    static func topViewController() -> UIViewController? {
        var presentingViewController = UIApplication.shared.keyWindow?.rootViewController
        while let presentedViewController = presentingViewController?.presentedViewController {
            presentingViewController = presentedViewController
        }
        return presentingViewController
    }
}

extension BMWShareManager {
    static func gotoSMS(_ contents: [BMWShareManager.ContentKey: Any]) {
        guard let text = contents[BMWShareManager.ContentKey.text] as? String else { return }
        ShareSMS.shareInstance.share(text)
    }
    
    static func gotoCopy(_ contents: [BMWShareManager.ContentKey: Any]) {
        guard let text = contents[BMWShareManager.ContentKey.text] as? String else { return }
        ShareCopy.share(text: text)
    }
}
